package com.gxa.controller.system;

import com.gxa.annotation.Perm;
import com.gxa.pojo.entity.system.User;
import com.gxa.service.system.UserService;
import com.gxa.utils.MessageCode;
import com.gxa.utils.QueryResult;
import com.gxa.utils.Result;
import com.gxa.utils.ResultUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author lch
 * @Date 2024/2/22 16:04
 * @Description
 **/
@RestController
public class UserController {

    @Resource
    private UserService userService;

    /**
     * 添加用户
     *
     * @param user
     * @return
     */
    @Perm("user:add")
    @PostMapping("/user/add")
    public Result addUser(User user) {
        int result = userService.addUser(user);
        if (result > 0) {
            return ResultUtils.buildSuccess(MessageCode.REQUEST_SUCCESS);
        }
        return ResultUtils.buildFail(MessageCode.SYSTEM_ERROR);
    }


    /**
     * 修改用户
     *
     * @param user
     * @return
     */
    @Perm("user:update")
    @PostMapping("/user/update")
    public Result updateUser(User user) {
        int result = userService.updateUser(user);
        if (result > 0) {
            return ResultUtils.buildSuccess(MessageCode.REQUEST_SUCCESS);
        }
        return ResultUtils.buildFail(MessageCode.SYSTEM_ERROR);
    }


    /**
     * 删除用户
     *
     * @param id 用户id
     * @return
     */
    @Perm("user:delete")
    @PostMapping("/user/delete")
    public Result deleteUser(Integer id) {
        int result = userService.deleteUserById(id);
        if (result > 0) {
            return ResultUtils.buildSuccess(MessageCode.REQUEST_SUCCESS);
        }
        return ResultUtils.buildFail(MessageCode.SYSTEM_ERROR);
    }


    /**
     * 查询用户
     *
     * @param user
     * @return
     */
    @GetMapping("/user/query")
    public Result queryUser(User user) {
        QueryResult result = userService.queryAllUser(user);
        return ResultUtils.buildSuccess(result.getData(), result.getCount());
    }


    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @Perm("user:batchDelete")
    @PostMapping("/user/batchDelete")
    public Result deleteByIds(@RequestParam("ids") List<Integer> ids) {
        int result = userService.batchDelete(ids);
        if (result > 0) {
            return ResultUtils.buildSuccess(MessageCode.REQUEST_SUCCESS);
        }
        return ResultUtils.buildFail(MessageCode.SYSTEM_ERROR);
    }
}
